package com.example.badyloans.config;

import java.math.BigDecimal;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 啊桥哦
 * @Description:
 * @date 2020-07-21 15:55
 */
public  class  CalculationLiXIJIS {
    /**
     * 得到计算总收益
     * @param borrowAmount
     * @param yearRate
     * @param repaymentMonth
     * @return
     */
    public static Long  calculationtotal(Long borrowAmount, Integer yearRate, Integer repaymentMonth, Integer repaymentType){
        Long totalInterest=null;
        switch (repaymentType){
            case 1:
                //先计算还款额度   借款本金*周期利润(1+周期利润)^还款周期/((1+周期利润)^还款周期-1)
                //借款总金额  本金
                BigDecimal borrowAmountBig = new BigDecimal(String.valueOf(borrowAmount));
                //计算周期利润
                BigDecimal monthRateBig = new BigDecimal(String.valueOf(yearRate))
                        .divide(new BigDecimal(String.valueOf(12)), 4, BigDecimal.ROUND_HALF_UP)
                        .divide(new BigDecimal(String.valueOf(100)), 4, BigDecimal.ROUND_HALF_UP);
                //还款周期
                BigDecimal repaymentMonthBig=new BigDecimal(String.valueOf(repaymentMonth));

                BigDecimal repayAmountPerMonth = borrowAmountBig
                        .multiply(monthRateBig)
                        .multiply(
                                BigDecimal.ONE.add(monthRateBig).pow(repaymentMonth)
                        ).divide(
                                BigDecimal.ONE.add(monthRateBig).pow(repaymentMonth).subtract(BigDecimal.ONE),4,BigDecimal.ROUND_HALF_UP
                        );
                //计算本息和
                //BigDecimal borrowAmountAndInterest = borrowAmountBig.multiply(BigDecimal.ONE.add(monthRateBig).pow(repaymentMonth));
                //本息和-本金
                BigDecimal totalInterestBig = repayAmountPerMonth.multiply(repaymentMonthBig).subtract(borrowAmountBig);
                totalInterest=  totalInterestBig.longValue();
                break;
            case 2:
                // 待完成
                break;
            default:
                break;
        }
        return totalInterest;
    }

    /**
     * 计算本息和，也就是总还款额
     * @param amount  借款额度  也就是本金
     * @return
     */
    public static BigDecimal calcuationTotalAmountAndInterset(Long amount, Integer yearRate, Integer repaymentMonth, Integer repaymentType){
        BigDecimal totalAmountAndInterset=BigDecimal.ZERO;
        switch (repaymentType){
            //等额本息
            case 1:
                 BigDecimal bigDecimalAmount=new BigDecimal(String.valueOf(amount));
                 //计算月利率，四舍五入到小数后4位
                     BigDecimal bigDecimalMonthRate=new BigDecimal(String.valueOf(yearRate))
                         //除以12  转换为每月利率
                         .divide(new BigDecimal(String.valueOf(12)),4,BigDecimal.ROUND_HALF_UP)
                         //除以100，去掉百分号
                         .divide(new BigDecimal(String.valueOf(100)),4,BigDecimal.ROUND_HALF_UP);
                //等额本息   本息和=本金*(1+月利率)^(期数-1)
                totalAmountAndInterset=bigDecimalAmount.multiply(
                        BigDecimal.ONE.add(bigDecimalMonthRate)
                        .pow(repaymentMonth - 1)
                );
                break;
                //先息后本
            case 2:
                break;
            default:
                break;
        }
        return totalAmountAndInterset;
    }

    /**
     * 计算每月还款额
     * @param yearRate
     * @param repaymentType
     * @param borrowAmount
     * @param repaymentMonth
     * @return
     */
    public static BigDecimal calculationRepaymentPerMonth(Integer yearRate, Integer repaymentType, Long borrowAmount, Integer repaymentMonth) {
        //先算出每个月还款额，根据公式 X = (借款总额*月利率*(1+月利率)^还款期数)/((1+月利率)^还款期数-1)
        //月利率，保留4位小数，并四舍五入
        BigDecimal monthRate = new BigDecimal(String.valueOf(yearRate))
        //先将整数转换成为百分比，因为前台只传递了百分比中的整数部分
                .divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_DOWN)
                //再除以12个月得到月利率
                .divide(BigDecimal.valueOf(12), 4, BigDecimal.ROUND_HALF_DOWN);
        BigDecimal repaymentPerMonth = BigDecimal.ZERO;
        //等额本息计算方法
        if (repaymentType.equals(1)) {
            //每月还款额
            repaymentPerMonth = (
                    //借款总额
                    BigDecimal.valueOf(borrowAmount)
                            //乘以月利率
                            .multiply(monthRate)
                            //乘以1+月利率的还款期数次幂
                            .multiply(BigDecimal.ONE.add(monthRate).pow(repaymentMonth)))
                    //除以 保留4位小数，并四舍五入
                    .divide(
                            BigDecimal.ONE.add(monthRate).pow(repaymentMonth).subtract(BigDecimal.ONE), 4, BigDecimal.ROUND_HALF_DOWN
                    );
        }
        return repaymentPerMonth;
    }

    /**
     * 每月待还本金
     *
     * @param yearRate
     * @param repaymentType
     * @param borrowAmount
     * @param repaymentMonth
     * @return
     *///调用上面的方法
    public static Long calculationPrincipalPerMonth(Integer yearRate, Integer repaymentType, Long borrowAmount, Integer repaymentMonth) {
        //月利率，保留4位小数，并四舍五入
        BigDecimal principalPerMonth = BigDecimal.ZERO;
        Long principalPerMonth1 = null;
        BigDecimal monthRate = new BigDecimal(String.valueOf(yearRate))
                //先将整数转换成为百分比，因为前台只传递了百分比中的整数部分
                .divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_DOWN)
                //再除以12个月得到月利率
                .divide(BigDecimal.valueOf(12), 4, BigDecimal.ROUND_HALF_DOWN);
        //每月还款额
        BigDecimal repaymentPerMonth = calculationRepaymentPerMonth(yearRate, repaymentType, borrowAmount, repaymentMonth);
        for (int i = 1; i <= repaymentMonth; i++) {
            //每月待还本金

            //等额本息
            if (repaymentType.equals(1)) {
                //先计算每个月应还利息=（上一月剩余应还本金-上一月利息）*月利率=待还本金*月利率*（1-月利率）^（还款期数-1）
                BigDecimal interestPerMonth = new BigDecimal(String.valueOf(borrowAmount))
                        .multiply(monthRate)
                        .multiply(
                                BigDecimal.ONE.subtract(monthRate)
                                        .pow(i - 1)
                        );
                //每月应还本金=每月还款额-每月待还利息
                principalPerMonth = repaymentPerMonth.subtract(interestPerMonth);
            }
        }
        principalPerMonth1=principalPerMonth.longValue();
        return principalPerMonth1;
    }

    /**
     * 计算每个月的待还利息
     *
     * @param yearRate
     * @param repaymentType
     * @param borrowAmount
     * @param repaymentMonth
     * @return
     */
    public static Long calculationInterestPerMonth(Integer yearRate, Integer repaymentType, Long borrowAmount, Integer repaymentMonth) {
        //月利率，保留4位小数，并四舍五入
        Long principalPerMonth1 = null;
         BigDecimal interestPerMonth = BigDecimal.ZERO;
        BigDecimal monthRate = new BigDecimal(String.valueOf(yearRate))
                //先将整数转换成为百分比，因为前台只传递了百分比中的整数部分
                .divide(BigDecimal.valueOf(100), 4, BigDecimal.ROUND_HALF_DOWN)
                //再除以12个月得到月利率
                .divide(BigDecimal.valueOf(12), 4, BigDecimal.ROUND_HALF_DOWN);
        for (int i = 1; i <= repaymentMonth; i++) {
            //等额本息
            if (repaymentType.equals(1)) {
                //先计算每个月应还利息=（上一月剩余应还本金-上一月利息）*月利率=待还本金*月利率*（1-月利率）^（还款期数-1）
                interestPerMonth = new BigDecimal(String.valueOf(borrowAmount))
                        .multiply(monthRate)
                        .multiply(
                                BigDecimal.ONE.subtract(monthRate)
                                        .pow(i - 1)
                        );
            }
        }
        principalPerMonth1=interestPerMonth.longValue();
        return principalPerMonth1;
    }
}        //每月还款额    calculationRepaymentPerMonth

